package com.catering.project.service.impl;

import java.util.List;
import java.util.Map;

import com.catering.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.catering.project.mapper.CartMapper;
import com.catering.project.domain.Cart;
import com.catering.project.service.ICartService;

/**
 * 购物车Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-18
 */
@Service
public class CartServiceImpl implements ICartService 
{
    @Autowired
    private CartMapper cartMapper;

    /**
     * 查询购物车
     * 
     * @param cartId 购物车主键
     * @return 购物车
     */
    @Override
    public Cart selectCartByCartId(Long cartId)
    {
        return cartMapper.selectCartByCartId(cartId);
    }

    /**
     * 查询购物车列表
     * 
     * @param cart 购物车
     * @return 购物车
     */
    @Override
    public List<Cart> selectCartList(Cart cart)
    {
        return cartMapper.selectCartList(cart);
    }

    /**
     * 新增购物车
     * 
     * @param cart 购物车
     * @return 结果
     */
    @Override
    public int insertCart(Cart cart)
    {
        return cartMapper.insertCart(cart);
    }

    /**
     * 修改购物车
     * 
     * @param cart 购物车
     * @return 结果
     */
    @Override
    public int updateCart(Cart cart)
    {
        return cartMapper.updateCart(cart);
    }

    /**
     * 批量删除购物车
     * 
     * @param cartIds 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteCartByCartIds(Long[] cartIds)
    {
        return cartMapper.deleteCartByCartIds(cartIds);
    }

    /**
     * 删除购物车信息
     * 
     * @param cartId 购物车主键
     * @return 结果
     */
    @Override
    public int deleteCartByCartId(Long cartId)
    {
        return cartMapper.deleteCartByCartId(cartId);
    }

    @Override
    public List<Map<String, Object>> selectCartList0(Integer tableId) {
        if (tableId == null) {
            return cartMapper.selectCartList0(tableId);
        } else {
            return cartMapper.selectCartList0(null);
        }

    }

    @Override
    public AjaxResult addDishList(Map<String, Object> map) {
        Integer tableId = (Integer) map.get("tableId");
        List<Map<String, Object>> dishList = (List<Map<String, Object>>) map.get("list");
        System.out.println(tableId);
        System.out.println(dishList);
        return AjaxResult.success(cartMapper.addDishList(tableId, dishList));
    }

    @Override
    public List<Cart> selectAllByTableIdCarts(Long tableId) {
        return cartMapper.selectAllByTableIdCarts(tableId);
    }
    @Override
    public boolean deleteCartByTableId(Long tableId) {
        int rowsAffected = cartMapper.deleteByTableId(tableId);
        return rowsAffected > 0;
    }

    //    通过商品id和桌子id更新购物车
    @Override
    public int updateCartByDidAndTid(Cart cart) {
        return cartMapper.updateCartByDidAndTid(cart);
    }


    @Override
    public void clearCartByTableId(Long tableId) {
        deleteCartByTableId(tableId);
    }
}
